import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { getParentQuestionList } from '@/service/module/main/classify/question'

interface IMenuListState {
  classifyParentList: any[]
}

const initialState: IMenuListState = {
  // 上级分类数据：新增或者编辑要获取
  classifyParentList: []
}

// 获取上级菜单数据
export const fetchParentQuestionListAction = createAsyncThunk(
  'fetch/parent-question-manage-list',
  async (type: number, { dispatch }) => {
    const res = await getParentQuestionList(type)
    dispatch(changeClassifyParentListAction(res.data))
    return res.data
  }
)

const questionSlice = createSlice({
  name: 'question-list',
  initialState,
  reducers: {
    changeClassifyParentListAction(state, { payload }) {
      state.classifyParentList = payload
    }
  }
})

export const { changeClassifyParentListAction } = questionSlice.actions
export default questionSlice.reducer
